//
//  WelcomeViewController.swift
//  weibo
//
//  Created by apple on 15/12/16.
//  Copyright © 2015年 LYR. All rights reserved.
//

import UIKit
import SnapKit
import SDWebImage


class WelcomeViewController: UIViewController {
   
     override func loadView() {
        
//        根视图等于背景视图
         view = backImageView
        
    }

    override func viewDidLoad() {
        super.viewDidLoad()

       //设置ui界面
        setupUI()
        
    }
    
    override func viewDidAppear(animated: Bool) {
        
        super.viewDidAppear(animated)
        // 开始动画
        startAnimation()
        
    }
    
    func setupUI() {
        
        //添加子控件
        view.addSubview(iconView)
        view.addSubview(welcomeLable)
        
        //头像图片添加约束
        iconView.snp_makeConstraints { (make) -> Void in
            
            make.centerX.equalTo(view.snp_centerX)
            make.bottom.equalTo(view.snp_bottom).offset(-200)
            
            //对size添加约束
            make.size.equalTo(CGSize(width: 90, height: 90))
            
        }
        
        // Lable 的约束
        welcomeLable.snp_makeConstraints { (make) -> Void in
            
            make.centerX.equalTo(iconView.snp_centerX)
            make.top.equalTo(iconView.snp_bottom).offset(16)
            
        }
        
        //  设置iconView  需要通过URL 地址去网络上下载
        iconView.sd_setImageWithURL(UserAccountViewModel().headImgURL, placeholderImage: UIImage(named: "avatar_default_big"))
        
        //设置头像图片的圆角
        iconView.layer.cornerRadius = 45
        
        //裁剪
        iconView.layer.masksToBounds = true
            
    }
    
//    执行动画  就是修改iconview 的约束
    private func startAnimation() {
        
        //把Lable的透明度设为0
        welcomeLable.alpha = 0
        
        //修改约束
      let offset =  -UIScreen.mainScreen().bounds.height + 200
        
        UIView.animateWithDuration(2.5, delay: 0, usingSpringWithDamping: 0.6, initialSpringVelocity: 6.0, options: [], animations: { () -> Void in
            
            
            
            self.iconView.snp_updateConstraints { (make) -> Void in
            
                //修改底部约束
                make.bottom.equalTo(self.view.snp_bottom).offset(offset)
                
            }

            //刷新页面
            self.view.layoutIfNeeded()
            
        }) { (_) -> Void in
            
            UIView.animateWithDuration(0.5, animations: { () -> Void in
                
                //头像显示出来后  再显示lable
                self.welcomeLable.alpha = 1
                
            }, completion: { (_) -> Void in
                
//                print("欢迎页面显示完毕")
            
                    //  发出通知  切换页面
                    NSNotificationCenter.defaultCenter().postNotificationName(WBSwitchRootVCNotification, object: nil)
                
            })
            
        }
   
    }
    
     // 懒加载所有视图
    private lazy var backImageView: UIImageView = UIImageView(image: UIImage(named: "ad_background"))
    
    private lazy var iconView: UIImageView = UIImageView()
   
    private lazy var welcomeLable: UILabel = {
    
        let l = UILabel()
        l.text = (UserAccountViewModel().userName ?? "") + " 欢迎归来"
        l.textColor = UIColor.blackColor()
        l.font = UIFont.systemFontOfSize(16)
        l.sizeToFit()
        
        return l
    }()

}
